
/* @description 正则验证
 * @param {String} val 要验证的值
 * @param {String} type 要验证的类型
 * [type可能的值为：notEmpty值不为空、phone手机号、sixFigures六位数字、身份证号码]
 * 根据需求继续扩展
 **/
export const u_Reg = (val, type) => {
    switch (type) {
        // 值不为空
        case 'notEmpty':
            return val && val.length > 0 && !/^\s/.test(val);
            break;
        // 验证手机号
        case 'phone':
            return /^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$/.test(val);
            break;
        // 六位数字  
        case 'sixFigures':
            return /^\d{6}$/.test(val);
            break;
        // 身份证号码  
        case 'idCard':
            return /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(val);
            break;
        default:
            alert('函数type参数不匹配');
            break;
    }
}

/* @description 格式化剩余时间
 * @param {String} endTime 结束日期
 * @return {Object} d:剩余天数、h:小时数、m:分钟数、s:秒数
 **/
export const u_formatRemainTime = (endTime, callback) => {
    const startDate = new Date(), //开始时间
        endDate = new Date(endTime), //结束时间
        t = endDate.getTime() - startDate.getTime(); //时间差
    let d = 0,
        h = 0,
        m = 0,
        s = 0;
    if (t <= 0) {
        callback && typeof callback === 'function' && callback();
    } else {
        d = formatNumber(Math.floor(t / 1000 / 60 / 60 / 24));
        h = formatNumber(Math.floor(t / 1000 / 60 / 60 % 24));
        m = formatNumber(Math.floor(t / 1000 / 60 % 60));
        s = formatNumber(Math.floor(t / 1000 % 60));
    }
    return {
         d: d, h: h, m: m, s: s
    }
}

export const seckill_1 = (rest) => {
    // let days = formatNumber(parseInt(rest / 60 / 60 / 24)),
    //   hours = formatNumber(parseInt(rest / 60 / 60 % 24)),
    //   minutes = formatNumber(parseInt(rest / 60 % 60)),
    //   seconds = formatNumber(parseInt(rest % 60));
	let nowTime = new Date(Date.parse(new Date()));
	let endTime = new Date(rest*1000);
	let t = endTime.getTime() - nowTime.getTime();
	let days = Math.floor(t/1000/60/60/24);
	let hours = Math.floor(t/1000/60/60%24);
	let minutes = Math.floor(t/1000/60%60);
	let seconds = Math.floor(t/1000%60);
	if(days < 10){
		days = '0' + days;
	}
	if(hours < 10){
		hours = '0' + hours;
	}
	if(minutes < 10){
		minutes = '0' + minutes;
	}
	if(seconds < 10){
		seconds = '0' + seconds;
	}
    return { d: days, h: hours, m: minutes, s: seconds }
  }
export const seckill_2 = (rest) => {
    let days = formatNumber(parseInt(rest / 60 / 60 / 24)),
      hours = formatNumber(parseInt(rest / 60 / 60 % 24)),
      minutes = formatNumber(parseInt(rest / 60 % 60)),
      seconds = formatNumber(parseInt(rest % 60));
    return { d: days, h: hours, m: minutes, s: seconds }
  }
/* 
秒转倒计时 
 */
export const timestampToDate = (ctime) => {


    let returnStr = '';

    // 天数位
    // let day = Math.floor(ctime / 3600 / 24);
    // let dayStr = day.toString();
    // if (dayStr.length == 1) dayStr = '0' + dayStr;

    // 小时位
    let hr = Math.floor(ctime / 3600);
    let hrStr = hr.toString();
    if (hrStr.length == 1) hrStr = '0' + hrStr;
    returnStr = hrStr;

    // 分钟位
    let min = Math.floor(ctime / 60 % 60);
    let minStr = min.toString();
    if (minStr.length == 1) minStr = '0' + minStr;
    returnStr += ':' + minStr;


    // 秒位
    let sec = Math.floor(ctime % 60);
    let secStr = sec.toString();
    if (secStr.length == 1) secStr = '0' + secStr;
    returnStr += ':' + secStr;


    return returnStr;
}
export const timestampToDate_1 = (ctime) => {


    let returnStr = '';

    // 天数位
    // let day = Math.floor(ctime / 3600 / 24);
    // let dayStr = day.toString();
    // if (dayStr.length == 1) dayStr = '0' + dayStr;

    // 小时位
    let hr = Math.floor(ctime / 3600);
    let hrStr = hr.toString();
    if (hrStr.length == 1) hrStr = '0' + hrStr;
    returnStr = hrStr;

    // 分钟位
    let min = Math.floor(ctime / 60 % 60);
    let minStr = min.toString();
    if (minStr.length == 1) minStr = '0' + minStr;
    returnStr += ':' + minStr;


    // 秒位
    let sec = Math.floor(ctime % 60);
    let secStr = sec.toString();
    if (secStr.length == 1) secStr = '0' + secStr;
    returnStr += ':' + secStr;


    return returnStr;
}  
/* @description 时间戳转换
 * @param {String} time 要转换的时间戳
 * @param {String} conf 配置参数
 * @return {Object} "all"返回年月日时分秒，"ymd"返回年月日，"ymdhm"返回年月日时分
 **/
export const u_formatTimestamp = (time, conf) => {
    let config = Object.assign({
        type: 'all', // 转换类型，"all"、"ymd"、"ymdhm"
        markFrot: "-", // 年月日的连接符，默认“-”
        markBack: ":" // 时分秒的连接符，默认“:”
    }, conf) ;
    let date = new Date(time),
        year = date.getFullYear(),
        month = formatNumber(date.getMonth() + 1),
        day = formatNumber(date.getDate()),
        hour = formatNumber(date.getHours()),
        minute = formatNumber(date.getMinutes()),
        second = formatNumber(date.getSeconds());
    switch (config.type) {
        case "ymd":
            return `${year}${config.markFrot}${month}${config.markFrot}${day}`;
            break;
        case "ymdhm":
            return `${year}${config.markFrot}${month}${config.markFrot}${day} ${hour}${config.markBack}${minute}`;
            break;
        default:
            return `${year}${config.markFrot}${month}${config.markFrot}${day} ${hour}${config.markBack}${minute}${config.markBack}${second}`;
            break;
    }  
}

// 将一位数转换成0开头，例如： 1->01，0->09
export const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}


/**
 * 将数字保留n位小数
*/
export const u_fixed = (val, n = 2) => {
    return (val - 0).toFixed(n);
}

export const u_throttle = (fn, delay, duration) => {                
    let timer = null;                
    let start = new Date();                    
    return function () {
        let current = new Date();
        const args = arguments;
        timer && clearTimeout(timer);
        if (current - start >= duration) {
            fn.apply(this, args);
            start = current;
        } else {
        timer = setTimeout(() => {
                fn.apply(this, args);
            }, delay);
        } 
    }
}

/** 
     * 日期解析，字符串转日期 
     * @param dateString 可以为2017-02-16，2017/02/16，2017.02.16 
     * @returns {Date} 返回对应的日期对象 
     */  
    function dateParse(dateString){  
        var SEPARATOR_BAR = "-";  
        var SEPARATOR_SLASH = "/";  
        var SEPARATOR_DOT = ".";  
        var dateArray;  
        if(dateString.indexOf(SEPARATOR_BAR) > -1){  
            dateArray = dateString.split(SEPARATOR_BAR);    
        }else if(dateString.indexOf(SEPARATOR_SLASH) > -1){  
            dateArray = dateString.split(SEPARATOR_SLASH);  
        }else{  
            dateArray = dateString.split(SEPARATOR_DOT);  
        }  
        return new Date(dateArray[0], dateArray[1]-1, dateArray[2]);   
    };  
      
    /** 
     * 日期比较大小 
     * compareDateString大于dateString，返回1； 
     * 等于返回0； 
     * compareDateString小于dateString，返回-1 
     * @param dateString 日期 
     * @param compareDateString 比较的日期 
     */  
    function dateCompare(dateString, compareDateString){  
        // if(isEmpty(dateString)){  
        //     alert("dateString不能为空");  
        //     return;  
        // }  
        // if(isEmpty(compareDateString)){  
        //     alert("compareDateString不能为空");  
        //     return;  
        // }  
        var dateTime = dateParse(dateString).getTime();  
        var compareDateTime = dateParse(compareDateString).getTime();  
        if(compareDateTime > dateTime){  
            return 1;  
        }else if(compareDateTime == dateTime){  
            return 0;  
        }else{  
            return -1;  
        }  
    };  
      
    /** 
     * 判断日期是否在区间内，在区间内返回true，否返回false 
     * @param dateString 日期字符串 
     * @param startDateString 区间开始日期字符串 
     * @param endDateString 区间结束日期字符串 
     * @returns {Number} 
     */  
export const isDateBetween = (dateString, startDateString, endDateString) => {  
        // if(isEmpty(dateString)){  
        //     alert("dateString不能为空");  
        //     return;  
        // }  
        // if(isEmpty(startDateString)){  
        //     alert("startDateString不能为空");  
        //     return;  
        // }  
        // if(isEmpty(endDateString)){  
        //     alert("endDateString不能为空");  
        //     return;  
        // }  
        var flag = false;  
        var startFlag = (dateCompare(dateString, startDateString) < 1);  
        var endFlag = (dateCompare(dateString, endDateString) > -1);  
        if(startFlag && endFlag){  
            flag = true;  
        }  
        return flag;  
    };  